package com.jinlia.show.modules.base.art_design.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.art_design.entity.VueRoute;
import com.jinlia.show.modules.base.art_design.mapper.VueRouteMapper;
import com.jinlia.show.modules.base.model.vo.SysMenuVO;
import com.jinlia.show.modules.base.system.entity.SysMenu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class VueRouteMapperService extends ServiceImpl<VueRouteMapper, VueRoute> {
    public List<VueRoute> getTreeVO(VueRoute menu) {
        List<VueRoute> rootSysMenuVOList = this.lambdaQuery()
                .list();
        //跟节点
        List<VueRoute> sysMenuVOList1 = buildTree(rootSysMenuVOList, 0l);
        return sysMenuVOList1;
    }
    /**
     * 构造父节点树
     *
     * @param listVO
     * @return
     */
    private List<VueRoute> buildTree(List<VueRoute> listVO, Long parentId) {
        Map<Long, VueRoute> idMap = listVO.stream().collect(Collectors.toMap(VueRoute::getId, i -> i));
        //认父节点
        List<VueRoute> rootFoldTree = new ArrayList<>();
        for (VueRoute foldTreeVO : listVO) {
            if (Objects.nonNull(foldTreeVO.getParentId()) && !foldTreeVO.getParentId().equals(parentId)) {
                VueRoute parentFoldTree = idMap.get(foldTreeVO.getParentId());//父元素
                if (parentFoldTree.getChildren() == null) {
                    parentFoldTree.setChildren(new ArrayList<>());
                }
                parentFoldTree.getChildren().add(foldTreeVO);//添加到父元素中
            }
            if ((parentId != null) && foldTreeVO.getId().equals(parentId)) {
                rootFoldTree.add(foldTreeVO);
            } else if (foldTreeVO.getParentId().equals(0l)) {
                //根节点
                rootFoldTree.add(foldTreeVO);
            }
        }
        return rootFoldTree;
    }

    /**
     * 添加或修改
     *
     * @param menu
     */
    public void addOrUpdate(VueRoute menu) {
        /**
         * 菜单名称不能重复
         */
        VueRoute oneSameName = this.lambdaQuery()
                .eq(VueRoute::getName, menu.getName())
                .one();
        /**
         * 菜单路径不能重复
         */
        VueRoute oneSamePath = this.lambdaQuery()
                .eq(VueRoute::getPath, menu.getPath())
                .one();
        if (menu.getId() == null) {
            if (oneSameName != null) {
                throw new RuntimeException("菜单名称已存在");
            }
            if (oneSamePath != null) {
                throw new RuntimeException("菜单路径已存在");
            }
            this.save(menu);
        } else {
            if (oneSameName != null && !oneSameName.getId().equals(menu.getId())) {
                throw new RuntimeException("菜单名称已存在");
            }
            if (oneSamePath != null && !oneSamePath.getId().equals(menu.getId())) {
                throw new RuntimeException("菜单路径已存在");
            }
            this.updateById(menu);
        }
    }

    public void delVueRoute(VueRoute menu) {
        this.removeById(menu);
    }
}
